<template>
    <el-container class="home_container">
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="heima">
                <span>电商管理系统</span>
            </div>
            <el-button type="info" @click="deleteToken">退出</el-button>
        </el-header>
        <!--            头部区域-->
        <el-container>

            <el-aside :width="isCollapse? '64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!--            弹框区域-->
                <el-menu background-color="#333744"
                         text-color="#fff" router :collapse="isCollapse"
                         unique-opened :collapse-transition="false"
                         :default-active="activepath">
                    <!--  一级菜单-->
                    <el-submenu :index="item.id + ''" v-for="item in MenuList" :key="item.id">
                        <!-- 一级菜单的模板 -->
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!--                            二级菜单-->
                        <el-menu-item :index="'/'+ subItem.path"
                                      v-for="subItem in item.children"
                                      @click="saveNavState('/'+ subItem.path)"
                                      :key="subItem.id">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!--            右侧主体区域-->
            <el-main>
                <!-- 路由占位符  展示路由的内容-->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        name: "Home",
        data() {
            return {
                MenuList: [],//左侧菜单数据
                iconsObj: {
                    '125': 'iconfont  icon-user',
                    '103': 'iconfont icon-tijikongjian',
                    '101': 'iconfont icon-shangpin',
                    '102': 'iconfont icon-danju',
                    '145': 'iconfont icon-baobiao'
                },
                isCollapse: false,
                activepath:'',//默认被激活的链接
            }
        },
        created() {
            this.getMenuList();
            this.activepath =  window.sessionStorage.getItem('activepath')
        },
        methods: {
            deleteToken() {
                //销毁token 返回登陆页
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            //获取左侧菜单数据
            async getMenuList() {
                const {data: res} = await this.$http.get('menus')
                // console.log(res)
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.MenuList = res.data
            },
            //折叠菜单
            toggleCollapse() {
                this.isCollapse = !this.isCollapse
            },
            //保存二级菜单激活的path路径
            saveNavState(activepath){
                window.sessionStorage.setItem('activepath',activepath)
                this.activepath = activepath
            }
        }
    }
</script>

<style lang="less" scoped>
    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #ffffff;

        > div {
            display: flex;
            align-items: center;
        }

        span {
            padding-left: 15px;
        }
    }

    .el-aside {
        background-color: #333744;
    }

    .el-main {
        background-color: #eaedf1;
    }

    .home_container {
        height: 100%;
    }

    .iconfont {
        margin-right: 10px;
    }

    .toggle-button {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.4em;
        cursor: pointer; //鼠标变成一个小手
    }
</style>